package com.ym.controller.management;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;

import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.ComplainRecordEntity;
import com.ym.medical.domain.param.ComplainRecordParam;
import com.ym.medical.service.IComplainRecordSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 申诉工单管理
 */
@Api(tags = "申诉工单管理")
@RestController
@RequestMapping("/management/complainRecord")
public class ComplainManagementController extends BaseController {

	@Autowired
	private IComplainRecordSV complainRecordSV;

	@ApiOperation("申诉工单列表展示")
	@GetMapping("/list")
	public APIResponse<PageInfo<ComplainRecordEntity>> listComplainRecord(
			@ApiParam(name = "complainRecordModel", value = "申诉工单实体", required = true)
					ComplainRecordParam complainRecordModel,
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "10") int limit) {
		PageHelper.startPage(pageNo,limit);
		List<ComplainRecordEntity> complainRecordPageInfo = complainRecordSV.selectList(complainRecordModel.covert());
		return successPage(complainRecordPageInfo);
	}

	@ApiOperation("审核工单")
	@GetMapping("/audit")
	public APIResponse auditComplainRecord(
			@ApiParam(name = "complainRecordDetail", value = "审核意见实体", required = true)
			@Validated(value = {ComplainRecordParam.Update.class})
					ComplainRecordParam complainRecord) {
		ComplainRecordEntity recordEntity  = complainRecordSV.selectById(complainRecord.getId());
		if (recordEntity == null) {
			throw new BusinessException("该审核工单不存在");
		}
		recordEntity.setAuditor(ShiroUtil.getLoginId());
		recordEntity.setAuditTime(new Date());
		complainRecordSV.auditComplainRecord(complainRecord);
		return APIResponse.success("审核工单成功", complainRecord.getId());
	}
}
